package admin

import (
	"chaowan/data_source"
	"chaowan/models"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Banners(ctx *gin.Context) {

	var users []models.Banner
	data_source.Db.Find(&users)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    users,
		"msg":     "ok",
	})
	return
}

type BannersBody struct {
	ID      int64  `json:"id" gorm:"id"`
	Image   string `json:"image" gorm:"image"`
	State   int64  `gorm:"column:state" json:"state"`
	Otherid int64  `gorm:"column:otherid" json:"otherid"`
	Index   int64  `gorm:"column:index" json:"index"`
	Content string `gorm:"column:content" json:"content"`
}

func Bannersadd(ctx *gin.Context) {
	request := BannersBody{}
	ctx.ShouldBind(&request)
	image := request.Image
	state := request.State
	otherid := request.Otherid
	index := request.Index
	content := request.Content
	gt := models.Banner{
		Image:   image,
		State:   state,
		Otherid: otherid,
		Index:   index,
		Content: content,
	}
	data_source.Db.Create(&gt)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Bannersupdate(ctx *gin.Context) {
	request := BannersBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	image := request.Image
	state := request.State
	otherid := request.Otherid
	index := request.Index
	content := request.Content
	gt := models.Banner{
		Image:   image,
		State:   state,
		Index:   index,
		Otherid: otherid,
		Content: content,
	}
	data_source.Db.Where("id = ?", id).Updates(&gt)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Bannersdel(ctx *gin.Context) {
	request := NiudanBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	data_source.Db.Where("id = ?", id).Delete(&models.Banner{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
